<template>
	<view class="main">
		<TopBar :indexflag="true" :combakflag="true" :lang="false" :bottom="true" :title="$t('MerchandiseManagement')"
			:barstyle="colortopic()" Color="#fff"></TopBar>
		<view class="shop-list" :dir="i18nLocale=='ug'?'rtl':''">
			<view class="shop-item" v-for="(item,index) in productList" v-if="!item.deleted_at" @click="getProductone(item.id)">
				<view class="left">
					<!-- <image :src="item.cover" mode=""></image> -->
					<image :src="getImgSrc(item.cover)" mode=""></image>
					<view class="shop-content">
						<view class="title">{{i18nLocale=='ug'?item.ug_name:item.cn_name}}</view>
						<view class="">￥{{item.price}}</view>
						<view class="sub-title">
							<text class="">{{$t('Sales')}} : {{item.sold}} </text>
						</view>
						<view class="sub-title">
							<text >{{$t('Category')}} : {{i18nLocale=='ug'?item.category.ug_name:item.category.cn_name}}</text>
						</view>
					</view>
				</view>
				<view class="right">
					<view class="button edit" @click.stop="editproduct(item.id)">
						<u-icon name="edit-pen" color="#f9ae3d" size="22"></u-icon>
						<text>{{$t('Edit')}}</text>
					</view>
					<view class="button edit" @click.stop="deletproduct(item.id)">
						<u-icon name="trash" color="#cc0000" size="22"></u-icon>
						<text>{{$t('Delete')}}</text>
					</view>
					<view class="button delet">
						<u-icon name="tags" :color="item.status?'#494FB6':'#cc0000'" size="22"></u-icon>
						<text class=""
							:style="{color:item.status?'#494FB6':'#cc0000'}">{{item.status? $t('Shelves'):$t('TakenShelves')}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="footer-button">
			<view class="button-item" style="border-right:1rpx solid #999" @click="addproduct(2)">
				<u-icon name="plus-circle" :color="colortopic()" size="22"></u-icon>
				<text>{{$t('ClassificationManagement')}}</text>
			</view>
			<view class="button-item" @click="addproduct(1)">
				<u-icon name="plus-circle" :color="colortopic()" size="22"></u-icon>
				<text>{{$t('AddShop')}}</text>
			</view>
		</view>
		<u-popup :show="popshow" :round="10" mode="center" @close="close" @open="open">
			<view :class="i18nLocale=='ug'?'ug-from pop-box' :'pop-box'" :dir="i18nLocale=='ug'?'rtl':''">
				<view class="pop-title">{{shopname}} {{$t('Edit')}}</view>
				<view class="input-item">
					<view class="title">
						{{$t('ShopPrice')}}
					</view>
					<u--input v-model="price" :placeholder="$t('ShopPricePlaceholder')" type="number" fontSize="28rpx"
						:adjustPosition='false'></u--input>
				</view>
				<view class="input-item">
					<view class="title">
						{{$t('ShopType')}}
					</view>
					<view class="" :style="{color:status?'#494FB6':colortopic()}">
						{{status? $t('Shelves'):$t('TakenShelves')}}
					</view>
					<view class="" style="">
						<switch :color="colortopic()" @change="change" :checked="status"
							style="transform:scale(0.7);direction: ltr;" />
					</view>
				</view>
				<view class="button-grop">
					<view class="button" :style="{color:colortopic()}" @click="editshop">{{$t('Confirm')}}</view>
					<view class="button" @click="popshow=false">{{$t('TimeCancel')}}</view>
				</view>
			</view>
		</u-popup>
		<Loading :show="loadshow" bgZindex="99" iconName="circle-round" :showText="false"></Loading>
		<u-toast ref="uToast" :message="notmessage"></u-toast>
	</view>
</template>

<script>
	import {
		getproducts,
		getproductone,
		putproducts,
		deleteproducts
	} from '@/api/shop.js';

	export default {
		data() {
			return {
				productList: [],
				notmessage: '请仔细填写!',
				popshow: false,
				status: false,
				price: '',
				shopname: '',
				shopid: '',
				pageSize: 1000,
				page: 1,
				loadshow: true,
			};
		},
		onShow() {
			this.getProducts()
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		methods: {
			getProducts() {
				this.loadshow = true;
				let that = this;
				getproducts({
					per_page: that.pageSize,
					page: that.page,
				}).then((res) => {
					if (res.code == 200) {
						that.loadshow = false;
						let data = res.body.data;
						that.productList = data;
					}
				})
			},
			//修改
			getProductone(id) {
				let that = this;
				that.shopid = id;
				getproductone({
					id: id
				}).then((res) => {
					if (res.code == 200) {
						let data = res.body.data;
						that.status = data.status;
						that.price = data.price;
						if (that.$i18n.locale == 'ug') {
							that.shopname = data.ug_name
						} else {
							that.shopname = data.cn_name
						}

					}
				})
				this.popshow = true
			},
			change(e) {
				this.status = e.detail.value;
			},
			//提交修改
			editshop() {
				let that = this;
				putproducts({
					id: that.shopid,
					status: that.status,
					price: that.price
				}).then((res) => {
					if (res.code == 200) {
						that.getProducts();
					}
				})
				this.popshow = false
			},
			open() {
				// console.log('open');
			},
			close() {
				this.popshow = false
			},
			//删除
			deletproduct(id) {
				let that=this;
				that.loadshow = true;
				deleteproducts({
					id:id
				}).then((res) => {
					if (res.code == 200) {
						this.getProducts();
					}
				})
			},	
			editproduct(id) {
				uni.navigateTo({
					url: '/pages/boos/menu/addshop?id=' + id
				})
			},
			//添加商品
			addproduct(type) {
				// type 1 是添加新菜   2是添加分类
				if (type == 1) {
					uni.navigateTo({
						url: '/pages/boos/menu/addshop'
					})
				} else {
					uni.navigateTo({
						url: '/pages/boos/menu/menucategory'
					})
				}

			}
		}
	}
</script>

<style lang="scss">
	.main {
		width: 100%;
		height: 100%;
		position: relative;
		padding-bottom: 120rpx;
	}

	.shop-list {
		width: 100%;
		height: 100%;

		.shop-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			background-color: $uni-bg-color-box;
			border-radius: $uni-border-radius-base;
			box-shadow: $uni-bg-shadow;
			margin-top: 30rpx;
			padding: $uni-spacing-padding-sm;

			.left {

				display: flex;
				align-items: center;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: $uni-border-radius-lg;
				}

				.shop-content {
					height: 200rpx;
					box-sizing: border-box;
					padding: 20rpx;

					.title {
						color: $uni-color-title;
						font-size: $uni-font-size-lg ;
					}

					.sub-title {
						color: $uni-text-color-grey;
						font-size: $uni-font-size-sm0;
						display: flex;
						justify-content: space-between;

					}

					display: flex;
					flex-direction: column;
					justify-content: space-around;
				}
			}

			.right {
				display: flex;
				align-items: center;
				color: $uni-text-color-grey;
				font-size: $uni-font-size-sm1;

				.button {
					padding: 5rpx 10rpx;
					border-radius: $uni-border-radius-lg;
					margin: 0rpx 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					text {
						padding: 10rpx 0rpx;
					}
				}

			}
		}

	}

	.footer-button {
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-around;
		position: fixed;
		bottom: 0;
		left: 0;

		.button-item {
			width: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		text {
			padding: 0rpx 20rpx;
		}
	}

	.pop-box {
		width: 600rpx;
		height: 500rpx;

		.pop-title {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0rpx;
		}

		.input-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: $uni-spacing-padding-sm 40rpx;
			box-sizing: border-box;
			margin-top: 20rpx;

			input {
				font-size: $uni-font-size-base;
				// border: 1rpx solid #999;
			}

			.title {
				color: $uni-color-title;
				font-size: $uni-font-size-base;
				padding: 0rpx 10rpx;
				// padding: 0rpx 30rpx;'
			}
		}

		.button-grop {
			display: flex;
			justify-content: space-around;
			margin-top: 50rpx;

			.button {
				border: 1rpx solid #999;
				padding: 10rpx 30rpx;
				border-radius: 20rpx;
			}
		}
	}

	.ug-from {
		direction: rtl;

		/deep/ .uni-input-input {
			direction: rtl !important;
			text-align: right;
		}

		/deep/ .uni-input-placeholder {
			text-align: right;
		}
	}
</style>